<template>
	<view style="padding: 20px;display: flex;flex-direction: column;justify-content: center;height: 100vh;">
		<button type="primary" loading @click="gotoNav" :disabled="!isReady">进入商城</button>
	</view>
</template>

<script>
	import psMethod from '@/common/psMethod';
	export default {
		data() {
			return {
				sConf: getApp().globalData,
				parent: null,
				isReady: false,
			}
		},
		onLoad(options) {
			const scene = decodeURIComponent(options.scene)
			console.log("staff onLoad --------------", scene);
			let parent = scene.split('=')[1]
			this.parent = parent

			let run = uni.getAccountInfoSync();
			if (run.miniProgram.envVersion === "release") {
				console.log('正式版');
				this.sConf.host = 'https://api.yuejunxi.com'
			} else if (run.miniProgram.envVersion === "trial") {
				console.log('体验版');
				this.sConf.host = 'https://dev.yuejunxi.com'
			} else if (run.miniProgram.envVersion === "develop") {
				console.log('开发板');
				this.sConf.host = 'https://dev.yuejunxi.com'
			} else {
				this.sConf.host = 'https://dev.yuejunxi.com'
			}
			this.sConf.pics = 'https://cos.yuejunxi.com'

			if (!this.sConf.auth) {
				this.autoLogin()
			} else {
				this.isReady = true
				this.bindParentStaff(this.parent).then(res => {
					console.log('已登录__检查绑定->', res);
				}).catch(err => {
					console.log('已登录__检查绑定=>', err);
				}).finally(() => {
					this.gotoNav()
				})
			}
			console.log('staff onLoad ed');
		},
		onReady() {
			console.log('staff onReady -----------------');
		},
		onShow() {
			console.log('staff onShow ------------------');
		},
		methods: {
			gotoNav() {
				uni.redirectTo({
					url: '/pages/index/index'
				})
			},
			async autoLogin() {
				console.log('无用户登录-自动登录');
				let code = await uni.login({
					provider: 'weixin',
					onlyAuthorize: true
				})
				console.log('wxCode->', code.code);
				let login = await this.hasWxCode(code.code).then(res => {
					console.log('登录完成->', res);
					this.sConf.auth = res.auth
					this.sConf.token = res.token
					this.isReady = true
					this.bindParentStaff(this.parent).then(res => {
						console.log('检查绑定->', res);
					}).catch(err => {
						console.log('检查绑定=>', err);
					}).finally(() => {
						this.gotoNav()
					})
				})

			},
			bindParentStaff(e) {
				// console.log('bindParentStaff', e);
				// console.log('检查绑定', Boolean(this.sConf.auth.parent), this.sConf.auth.parent);
				// console.log('检查绑定', e, this.sConf.auth.id);
				return new Promise((resolve, reject) => {
					if (!Boolean(this.sConf.auth.parent) && Boolean(e) && this.sConf.auth.id !== e) {
						let host = this.sConf.host + '/weapps/bindHelp/';
						let token = this.sConf.token || ''
						let data = {
							parent: e
						}
						psMethod.authenticatorPostSync(host, token, data).then(res => {
							console.log('绑定推荐', res);
							resolve(res.resmsg)
						}).catch(err => {
							reject(err)
						})
					} else {
						reject('不需要绑定')
					}
				})
			},

			hasWxCode(e) {
				let host = this.sConf.host + '/weapps/login/'
				let data = {
					code: e
				}
				return new Promise((resolve, reject) => {
					psMethod.requestPostSync(host, data).then(res => {
						console.log('微信快速登录->', res);
						resolve(res.resmsg)
					}).catch(err => {
						reject(err)
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>